// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/house.dart';
import 'package:flutter_house_manager/utils/Emitter.dart';
import 'package:get/get.dart';

class HouseDetail extends StatefulWidget {
  const HouseDetail({super.key});

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  Widget tagBuilder(int status) {
    List tagList = [
      {},
      {"bgColor": const Color.fromARGB(50, 91, 177, 227), "textColor": const Color.fromARGB(255, 85, 145, 175), "title": "审核中"},
      {"bgColor": const Color.fromARGB(255, 91, 243, 91), "textColor": const Color.fromRGBO(1, 50, 1, 1), "title": "审核成功"},
      {"bgColor": const Color.fromARGB(255, 247, 129, 133), "textColor": const Color.fromARGB(255, 84, 1, 1), "title": "审核失败"}
    ];

    return Container(
      padding: const EdgeInsets.only(left: 5, right: 5, top: 2, bottom: 2),
      decoration: BoxDecoration(
        color: tagList[status]['bgColor'],
        borderRadius: BorderRadius.circular(5),
      ),
      child: Text(
        '${tagList[status]['title']}',
        style: TextStyle(color: tagList[status]['textColor']),
      ),
    );
  }

  Map<String, dynamic> detail = {};

  bool isLoading = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final params = ModalRoute.of(context)?.settings.arguments as Map;
    if (params["id"] != null && params["id"] != "") {
      getHouseDetail(params["id"]);
    }
  }

  getHouseDetail(String id) async {
    setState(() {
      isLoading = true;
    });
    detail = await getHouseDetailAPI(id);
    setState(() {
      isLoading = false;
    });
  }

  _showDeleteDialog() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(title: const Text('提示'), content: const Text('确定要删除吗？'), actions: [
            TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text('取消')),
            TextButton(
                onPressed: () async {
                  // 调用接口
                  await delHouseAPI(detail["id"]);
                  // 通知刷新房屋列表
                  eventBus.fire(RefreshHouseListEvent());
                  // 关闭弹层
                  Navigator.of(context).pop();
                  // 回到上一个页面
                  Navigator.of(context).pop();
                },
                child: const Text('确定', style: TextStyle(color: Colors.red))),
          ]);
        });
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }
    return Scaffold(
      appBar: AppBar(
        title: const Text('房源详情'),
        centerTitle: true,
      ),
      body: Stack(
        alignment: Alignment.bottomCenter,
        children: [
          ListView(
            padding: const EdgeInsets.only(bottom: 100),
            children: [
              // 房屋信息
              Container(
                padding: const EdgeInsets.all(10),
                child: const Text('房屋信息', style: TextStyle(color: Color.fromARGB(255, 97, 94, 94), fontSize: 16)),
              ),
              Container(
                color: Colors.white,
                padding: const EdgeInsets.only(left: 10, right: 10, top: 15, bottom: 15),
                child: Row(
                  children: [
                    Expanded(child: Text(detail["point"] + detail["building"] + detail["room"])),
                    tagBuilder(1),
                  ],
                ),
              ),
              // 业主信息
              Container(
                padding: const EdgeInsets.all(10),
                child: const Text('业主信息', style: TextStyle(color: Color.fromARGB(255, 97, 94, 94), fontSize: 16)),
              ),
              Container(
                  color: Colors.white,
                  padding: const EdgeInsets.only(left: 10, right: 10, top: 15, bottom: 15),
                  child: Row(
                    children: [
                      const SizedBox(
                        width: 60,
                        child: Text('房间号'),
                      ),
                      const SizedBox(width: 30),
                      Expanded(child: Text(detail["room"]))
                    ],
                  )),
              Container(
                  color: Colors.white,
                  padding: const EdgeInsets.only(left: 10, right: 10, top: 15, bottom: 15),
                  child: Row(
                    children: [
                      const SizedBox(width: 60, child: Text('业主')),
                      const SizedBox(width: 30),
                      Expanded(child: Text(detail["name"])),
                    ],
                  )),
              Container(
                  color: Colors.white,
                  padding: const EdgeInsets.only(left: 10, right: 10, top: 15, bottom: 15),
                  child: Row(
                    children: [
                      const SizedBox(width: 60, child: Text('手机号')),
                      const SizedBox(width: 30),
                      Expanded(
                        child: Text(detail["mobile"]),
                      )
                    ],
                  )),
              const SizedBox(height: 30),
              Container(
                padding: const EdgeInsets.all(10),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text('本人身份证照片'),
                    Image.network(detail["idcardFrontUrl"], height: 200),
                    Image.network(detail["idcardBackUrl"], height: 200),
                  ],
                ),
              ),
            ],
          ),
          Container(
            padding: const EdgeInsets.all(10),
            width: MediaQuery.of(context).size.width,
            height: 80,
            child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
              ElevatedButton(
                  onPressed: () {
                    _showDeleteDialog();
                  },
                  child: const Column(
                    children: [
                      SizedBox(height: 8, width: 120),
                      Icon(Icons.delete),
                      Text('删除房屋'),
                      SizedBox(height: 8),
                    ],
                  )),
              ElevatedButton(
                onPressed: () {
                  Navigator.pushNamed(context, '/house_form', arguments: {
                    'id': detail["id"],
                  });
                },
                child: const Column(
                  children: [
                    SizedBox(
                      height: 8,
                      width: 120,
                    ),
                    Icon(Icons.edit),
                    Text('修改房屋'),
                    SizedBox(height: 8)
                  ],
                ),
              )
            ]),
          )
        ],
      ),
    );
  }
}
